<script setup>
import {useRouter} from "vue-router";
import {onMounted, ref} from "vue";
import Http from "@/http/http.js";
import moment from "moment";
import {Message} from "@arco-design/web-vue";
import {getUserInfoBase} from "@/stores/user.js";
import Editor from "@/components/editor.vue";

const router = useRouter()
const userParams = router.currentRoute.value.query
const userData = getUserInfoBase()
const editor = ref()
const companyData = ref([])

const userInfo = ref({
  UserId: 0,
  ProjectId: 0,
  PermissionId: 3,
  UserSex: 0,
  UserPassword: "",
  UserName: "",
  UserEmail: "",
  UserPhone: "",
  UserAccount: "",
  UserInfo: "",
  Wechat: "",
  UserNumber: "教师",
  CreateTime: moment(),
})

onMounted(() => {
  userInfo.value.UserInfo = editor.value.content
  getParams()
})

const getParams = () => {
  if (userParams.UserId && userParams.UserId > 0) {
    getUserInfo()
  } else {
    userInfo.value.UserId = Number(userParams.UserId);
    userInfo.value.PermissionId = Number(userParams.PermissionId);
  }
}
const getUserInfo = async () => {
  const res = await Http.userOne({id: userParams.UserId});
  if (res.Data) {
    userInfo.value = res.Data[0];
  }
  userInfo.value.CreateTime = moment(userInfo.value.CreateTime)
}



const saveData = async () => {
  if (!userInfo.value.UserAccount) {
    Message.error("账号填写错误!");
    return;
  }

  if (!userInfo.value.UserPhone) {
    Message.error("手机号填写错误!");
    return;
  } else {
    if (userInfo.value.UserPhone.length !== 11) {
      Message.error("手机号填写错误!");
      return;
    }
  }

  if (!userInfo.value.UserName) {
    Message.error("姓名填写错误!");
    return;
  } else {
    if (userInfo.value.UserName.length < 3) {
      Message.error("姓名填写错误!");
      return;
    }
  }
  let Obj = {};
  Obj["AuthNum"] = userInfo.value.AuthNum;
  Obj["UserPhone"] = userInfo.value.UserPhone;
  Obj["UserName"] = userInfo.value.UserName;
  Obj["UserSex"] = userInfo.value.UserSex;
  Obj["UserNumber"] = userInfo.value.UserNumber;
  Obj["UserEmail"] = userInfo.value.UserEmail;
  if (userInfo.value.Wechat !== "") {
    Obj["Wechat"] = userInfo.value.Wechat;
  }
  Obj["PermissionId"] = userInfo.value.PermissionId;
  Obj["ProjectId"] = userInfo.value.ProjectId;
  if (editor.value.content !== "") {
    Obj["UserInfo"] = editor.value.content
  }
  if (Number(userParams.UserId) > 0) {
    Obj["UserId"] = userInfo.value.UserId;
    const res = await Http.editUser(Obj);
    if (res["Msg"] !== "OK") {
      Message.success("编辑成功");
    }
  } else {
    if (await checkAccount(null)) {
      return
    }
    Obj["UserAccount"] = userInfo.value.UserAccount;
    Obj["UserPassword"] = userInfo.value.UserPassword;
    Obj["AdderId"] = Number(userData.UserId);
    Obj["TeachName"] = userData.UserName;
    const res = await Http.addUser({addUser: [Obj]});
    if (res["Msg"] !== "OK") {
      Message.success(res.Data);
    }
  }
  setTimeout(() => {
    backUrl()
  }, 1000);
}

const checkAccount = async (data) => {
  if (!userInfo.value.UserAccount) return
  const res = await Http.checkAccount({UserAccount: userInfo.value.UserAccount});
  if (!res.Data) {
    if (data) {
      Message.success("恭喜您,账号可以使用!");
    }
  } else {
    Message.error("账号已存在,不可以使用!");
  }
  return res.Data
}

const backUrl = () => {
  router.push(`/member/userList`)
}
</script>

<template>
  <a-card>
    <a-form :model="userInfo" auto-label-width>
      <a-form-item label="帐号" required>
        <a-space>
          <a-input style="width: 320px" v-model="userInfo.UserAccount" placeholder="帐号" :disabled="Number(userInfo.UserId) !== 0"/>
          <a-button v-if="Number(userInfo.UserId) === 0" @click="checkAccount(1)" :type="userInfo.UserAccount.length>4?'primary':'dashed'">检测是否可用</a-button>
        </a-space>
      </a-form-item>
      <a-form-item v-if="userInfo.UserId === 0" label="密码" required>
        <a-input-password v-model="userInfo.UserPassword" placeholder="密码"/>
      </a-form-item>
      <a-form-item label="手机" required>
        <a-input v-model="userInfo.UserPhone" :max-length="11" placeholder="手机" show-word-limit/>
      </a-form-item>
      <a-form-item label="姓名" required>
        <a-input v-model="userInfo.UserName" placeholder="姓名"/>
      </a-form-item>
      <a-form-item label="性别">
        <a-select v-model="userInfo.UserSex" placeholder="性别">
          <a-option :value="1">男</a-option>
          <a-option :value="0">女</a-option>
        </a-select>
      </a-form-item>
      <a-form-item label="邮箱">
        <a-input v-model="userInfo.UserEmail" placeholder="邮箱"/>
      </a-form-item>
      <a-form-item label="微信">
        <a-input v-model="userInfo.Wechat" placeholder="微信"/>
      </a-form-item>
      <a-form-item label="个人简介">
        <editor :value="userInfo.UserInfo" ref="editor"></editor>
      </a-form-item>
      <a-form-item label="时间" v-if="userInfo.UserId > 0">
        <a-date-picker v-model="userInfo['CreateTime']" format="YYYY-MM-DD hh:mm:ss" disabled/>
      </a-form-item>
      <a-form-item>
        <a-space>
          <a-button type="primary" @click="saveData()">{{ userInfo.UserId === 0 ? "提交" : "编辑" }}</a-button>
          <a-button @click="backUrl()">返回</a-button>
        </a-space>
      </a-form-item>
    </a-form>
  </a-card>
</template>

<style scoped lang="less">

</style>
